#include <iostream>
#include <string>
#include <XService.h>
#include <thread_pool/XThreadPool.h>
#include "XDirServiceHandle.h"
#include <register/register_client/xregister_client.h>
#include <config/config_client/XConfigClient.h>
#include <XMsg.h>
#include <xplatform/XTools.h>
#include <thread>
#include <xplatform/protobuf/xmsg_com.pb.h>
#include "XDirConfig.pb.h"
using namespace std;
class XTestService : public XService
{
public:
    XServiceHandle *CreateServiceHandle() override
    {
        return new XDirServiceHandle();
    }
};
int main(int argc, char *argv[])
{
    cout << "test_config_gateway_register_server SERVER_PORT REGISTER_IP REGISTER_PORT" << endl;
    int server_port = 20011;
    if (argc > 1)
        server_port = atoi(argv[1]);
    cout << "server port is " << server_port << endl;

    string register_ip = "127.0.0.1";
    if (argc > 2)
        register_ip = argv[2];
    int register_port = REGISTER_PORT;
    if (argc > 3)
        register_port = atoi(argv[3]);
    XRegisterClient::MsgFuncCallback();
    // 设置注册中心的IP和端口
    XRegisterClient::Get()->setServerIP(register_ip.c_str());
    XRegisterClient::Get()->setServerPort(register_port);

    // 注册到注册中心
    XRegisterClient::Get()->RegisterServer("dir", server_port, 0);

    auto configs = XRegisterClient::Get()->GetService(CONFIG_NAME);
    cout << "configs:" << configs.DebugString() << std::endl;
    if (configs.services_size() <= 0)
    {
        cout << "no configs" << endl;
    }
    else
    {
        // 取第一个配置中心
        auto config = configs.services(0);
        // XConfigClient::RegMsgCallback();
        // XConfigClient::Get()->setServerIP(config.ip().c_str());
        // XConfigClient::Get()->setServerPort(config.port());
        // XConfigClient::Get()->StartConnect();
        XDirConfig dir_conf;
        XConfigClient::Get()->StartGetConfig(config.ip().c_str(), config.port(),
                                             "127.0.0.1", server_port, &dir_conf);
        if (!XConfigClient::Get()->waitConnected(10))
        {
            cout << "connect config server failed" << endl;
        }
        else
        {
            cout << "connect config server succeed" << endl;
            xmsg::XConfig config;
            config.set_service_name("config_gateway_register_server");
            config.set_service_ip("127.0.0.1");
            config.set_service_port(server_port);
            config.set_proto("test_config_gateway_register_server");
            XConfigClient::Get()->SaveConfig(&config);
            this_thread::sleep_for(chrono::seconds(1));
            // XConfigClient::Get()->SaveConfig();
            XConfigClient::Get()->LoadConfig("127.0.0.1", server_port); //"127.0.0.1",20012
            std::this_thread::sleep_for(std::chrono::milliseconds(300));
            xmsg::XConfig tem_conf;
            int i = 0;
            while (!XConfigClient::Get()->GetConfig("127.0.0.1", server_port, &tem_conf))
            {
                std::cout << i++ << std::endl;
                std::this_thread::sleep_for(std::chrono::milliseconds(300));
            }
            cout << "tem_conf:" << tem_conf.DebugString() << endl;
        }
    }
    // 注册消息回调函数
    XDirServiceHandle::RegMsgCallback();

    XTestService service;
    service.set_server_port_(server_port);
    service.set_thread_count_(1);
    service.Start();
    XThreadPool::Wait();
}